﻿<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>设备列表</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <div th:include="layout/resources :: resources"></div>
</head>
<body>
<div class="container-fluid">
    <div id="toolbar">
        <label>修改组</label>
        <select id="group" class="form-control" style="display: inline;width: auto;" onchange="updateGroup();">
            <option value="">---请选择---</option>
        </select>
    </div>
    <table></table>
</div>

</body>
<script>

    $(function () {
        init();
    })
    function init() {
        $.ajax({
            method: "get",
            url: createUrl("/group/getListByPage"),
            success: function (result) {
                for (i in result.list) {
                    $("#group").append('<option value="' + result.list[i].id + '">' + result.list[i].groupName + '</option>');
                }
            }
        })
    }
    $("table").framework().bootstrapTable({
        url: "/deviceInfo/getListByPage",
        columns: [{
            checkbox: true
        }, {
            field: '',
            title: '序号',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return index + 1;
            }
        }, {
            field: 'id',
            title: '操作',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<a href="javascript:deleteOne(' + value + ');">删除</a>';
            }
        }, {
            field: 'deviceId',
            title: '设备ID',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'groupName',
            title: '组名',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'createTime',
            title: '创建时间',
            align: 'center',
            valign: 'middle'
        }]
    });

    function refresh() {
        $('table').bootstrapTable('refresh');
    }

    function deleteOne(id) {
        $.ajax({
            method: "delete",
            url: createUrl("/deviceInfo/delete/" + id),
            success: function (result) {
                if (result.errorCode == 0) {
                    framework.successMsg("删除成功");
                } else {
                    framework.errorMsg("删除失败")
                }
            }
        })
    }

    function updateGroup() {
        var selected = $('table').bootstrapTable('getSelections');
        var ids = [];
        for (i in selected) {
            ids.push(selected[i].id)
        }
        $.ajax({
            method: "post",
            url: createUrl("/deviceInfo/updateGroup"),
            data: JSON.stringify({
                groupId: $("#group").val(),
                ids: ids
            }),
            dataType: "json",
            contentType: "application/json",
            success: function (result) {
                if (result.errorCode == 0) {
                    framework.successMsg("修改成功");
                } else {
                    framework.errorMsg("修改失败")
                }
            }
        })
    }
</script>
</html>
